package com.market.controller.admin;

import com.market.bean.MarketAdmin;
import com.market.bean.bo.admin.AdminPasswordBo;
import com.market.bean.common.BaseRespVo;
import com.market.service.admin.AdminAdminService;
import com.market.utils.StringUtil;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author fan
 * @Description:
 * @since 2023/01/02 19:26
 */
@RestController
@RequestMapping("admin/profile")
public class AdminProfileController {
    @Autowired
    AdminAdminService adminService;

    @PostMapping("password")
    public BaseRespVo password(@RequestBody AdminPasswordBo adminPasswordBo) {
        MarketAdmin admin = (MarketAdmin) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        if (!admin.getPassword().equals(adminPasswordBo.getOldPassword())) {
            return BaseRespVo.invalidParameter("原密码错误");
        }
        if (adminPasswordBo.getNewPassword().length()<6){
            return BaseRespVo.invalidParameter("密码长度不能小于6");
        }
        if (StringUtil.isEmpty(adminPasswordBo.getNewPassword()) || !adminPasswordBo.getNewPassword().equals(adminPasswordBo.getNewPassword2())) {
            return BaseRespVo.invalidParameter("两次密码不一致");
        }

        admin.setPassword(adminPasswordBo.getNewPassword());
        adminService.password(admin);
        return BaseRespVo.ok();
    }
}
